package javafx179labs.gusev.lab4;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.input.MouseEvent;
import javafx.scene.effect.Lighting;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.transform.Rotate;
import javafx.scene.shape.Circle;
import javafx.animation.Timeline;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.animation.KeyFrame;

//мультик еще ни разу не мультик, но, тем не менее, кое-какие наработки есть
var fr1 : Text;
var fr2 : Text;
var fr3 : Text;
var fr4 : Text;
var lopata : Text;
var coolmult1 : Group;
var angle : Number;

var face1 = ImageView
{
    image: Image { url: "{__DIR__}face1.JPG" }
    x: 270
    y: -200
}

var rhand1 = ImageView
{
    image: Image { url: "{__DIR__}rhand1.JPG" }
    x: bind face1.x-140
    y: bind face1.y+20
    transforms:
    [
        Rotate
        {
            pivotX : bind face1.x
            pivotY : bind face1.y
            angle: bind angle
        }
    ]
}

var lhand1 = ImageView
{
    image: Image { url: "{__DIR__}lhand1.JPG" }
    x: bind face1.x+140
    y: bind face1.y+20
    transforms:
    [
        Rotate
        {
            pivotX : bind face1.x+140
            pivotY : bind face1.y
            angle: bind -angle
        }
    ]
}

var face2 = ImageView
{
    image: Image { url: "{__DIR__}face2.JPG" }
    y: 250
    x: -360
}

var rhand2 = ImageView
{
    image: Image { url: "{__DIR__}rhand2.JPG" }
    y: bind face2.y+20
    x: bind face2.x-140
    transforms:
    [
        Rotate
        {
            pivotX : bind face2.x
            pivotY : bind face2.y
            angle: bind angle
        }
    ]
}

var lhand2 = ImageView
{
    image: Image { url: "{__DIR__}lhand2.JPG" }
    y: bind face2.y+20
    x: bind face2.x+140
    transforms:
    [
        Rotate
        {
            pivotX : bind face2.x+140
            pivotY : bind face2.y
            angle: bind -angle
        }
    ]
}

var face3 = ImageView
{
    image: Image { url: "{__DIR__}face3.JPG" }
    x: 400
    y: 700
}

var rhand3 = ImageView
{
    image: Image { url: "{__DIR__}rhand3.JPG" }
    x: bind face3.x-140
    y: bind face3.y+20
    transforms:
    [
        Rotate
        {
            pivotX : bind face3.x
            pivotY : bind face3.y
            angle: bind angle
        }
    ]
}

var lhand3 = ImageView
{
    image: Image { url: "{__DIR__}lhand3.JPG" }
    x: bind face3.x+140
    y: bind face3.y+20
    transforms:
    [
        Rotate
        {
            pivotX : bind face3.x+140
            pivotY : bind face3.y
            angle: bind -angle
        }
    ]
}

var begin : Timeline = Timeline
{
    keyFrames :
    [
        at (3s)
        {
            face1.y => 100;
            face2.x => 140;
            face3.y => 400;
        }

        KeyFrame
        {
                time: 4s
                action: function() { insert fr1 into coolmult1.content; }
        }

        KeyFrame
        {
                time: 6s
                action: function() { delete fr1 from coolmult1.content; }
        }

        KeyFrame
        {
                time: 6s
                action: function() { insert fr2 into coolmult1.content; }
        }

        KeyFrame
        {
                time: 8s
                action: function() { delete fr2 from coolmult1.content; }
        }

        KeyFrame
        {
                time: 8s
                action: function() { insert fr3 into coolmult1.content; }
        }

        KeyFrame
        {
                time: 10s
                action: function() { delete fr3 from coolmult1.content; }
        }

        KeyFrame
        {
                time: 10s
                action: function() { insert fr4 into coolmult1.content; }
        }

        KeyFrame
        {
                time: 12s
                action: function() { delete fr4 from coolmult1.content; }
        }

        KeyFrame
        {
                time: 12s
                action: function() { insert lopata into coolmult1.content; }
        }
    ]
}

var hands : Timeline = Timeline
{
    repeatCount: Timeline.INDEFINITE
    keyFrames :
    [
        at (0.5s) { angle => 45; }
        at (1s) { angle => 0; }
        at (1.5s) { angle => -45; }
        at (2s) { angle => 0; }
    ]
}

fr1 = Text
{
    font: Font { size: 25 }
    x: 200
    y: 30
    content: "Я Наполеон!"
}

fr2 = Text
{
    font: Font { size: 25 }
    x: 20
    y: 400
    content: "Кто тебе сказал?"
}

fr3 = Text
{
    font: Font { size: 25 }
    x: 200
    y: 30
    content: "Бог!"
}

fr4 = Text
{
    font: Font { size: 25 }
    x: 400
    y: 600
    content: "Я такого не говорил!"
}

lopata = Text
{
    font: Font { size: 80 }
    x: 200
    y: 400
    content: "ЛОПАТА"
}

var watchmult : Text = Text
{
    font : Font { size: 20 }
    x: 480
    y: 500
    content: "Позырить\n мультик"
}

var environment : Group = Group
{
    content:
    [
        Rectangle
        {
            x: 0
            y: 400
            width: 700
            height: 300
            fill: Color.GREEN
        }

        Rectangle
        {
            x: 340
            y: 600
            width: 90
            height: 100
            fill: Color.GRAY
        }

        Circle
        {
            centerX: 100
            centerY: 100
            radius: 40
            fill: Color.YELLOW
        }
    ]
}

var psihbol: Group = Group
{
    content:
    [
        Rectangle
        {
            x: 300
            y: 300
            width: 300
            height: 300
            fill: Color.YELLOW
        }

        Polygon
        {
            points : [ 250,300, 450,200, 650,300 ]
            fill: Color.RED
        }

        Rectangle
        {
            x: 350
            y: 470
            width: 70
            height: 130
            fill: Color.BROWN
        }

        Rectangle
        {
            x: 500
            y: 350
            width: 70
            height: 70
            fill: Color.BLUE
        }

        Rectangle
        {
            x: 350
            y: 350
            width: 70
            height: 70
            fill: Color.BLUE
        }

        Text
        {
            font : Font { size: 40 }
            x: 100
            y: 250
            content: "Психбольница №8"
            transforms: Rotate
            {
                pivotX : 300.0
                pivotY : 100.0
                angle: -27.0
            }
        }
    ]

    onMouseEntered: function( e: MouseEvent ):Void
    {
        for (i in [0..5]){
        psihbol.content[i].effect = Lighting {
                        diffuseConstant: 1.0
                        specularConstant: 1.0
                        specularExponent: 20
                        surfaceScale: 1.5
                }
        }
        insert watchmult into psihbol.content;
    }

    onMouseExited: function( e: MouseEvent ):Void
    {
        for (i in [0..5]){
        psihbol.content[i].effect = null;
        }
        delete watchmult from psihbol.content;
    }

    onMouseClicked: function( e: MouseEvent ):Void
    {
        delete psihbol from st.scene.content;
        delete environment from st.scene.content;
        insert coolmult1 into st.scene.content;
        begin.playFromStart();
        hands.play();
    }
}

coolmult1 = Group
{
    content:
    [
        rhand1, rhand2, rhand3, lhand1, lhand2, lhand3, face1, face2, face3
    ]
}

var st: Stage = Stage {
    title: "Application title"
    width: 700
    height: 700
    scene: Scene
    {
        content:
        [
            environment,
            psihbol
        ]
    }
}